'use strict';

const moment = require('moment');

module.exports = app => {
  const { Sequelize } = app;

  const Integrals = app.model.define('Integrals', {
    id: {
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
      type: Sequelize.INTEGER
    },
    userId: {
      type: Sequelize.INTEGER,
      allowNull: false,
      onDelete: "CASCADE",
      onUpdate: "CASCADE",
      references: {
        model: 'Users',
        key: 'id'
      }
    },
    amount: {
      allowNull: false,
      type: Sequelize.INTEGER(11).UNSIGNED,
      comment: '余量'
    },
    number: {
      allowNull: false,
      type: Sequelize.INTEGER,
      comment: '指出收入记录'
    },
    totalIn: {
      allowNull: false,
      type: Sequelize.INTEGER,
      comment: '收入总数量'
    },
    totalOut: {
      allowNull: false,
      type: Sequelize.INTEGER,
      comment: '支出总数量'
    },
    type: {
      allowNull: false,
      type: Sequelize.INTEGER(1),
      comment: '来源',
    },
    createdAt: {
      allowNull: false,
      type: Sequelize.DATE,
      get() { return moment(this.getDataValue('createdAt')).format('YYYY-MM-DD HH:mm') }
    },
    updatedAt: {
      allowNull: false,
      type: Sequelize.DATE,
      get() { return moment(this.getDataValue('updatedAt')).format('YYYY-MM-DD HH:mm') }
    },
  }, {
    paranoid: false,
    charset: 'utf8mb4',
    collate: 'utf8mb4_general_ci',
    comment: '幸运草收支表',
  });

  Integrals.associate = models => {
    // Integrals.hasMany(app.model.IntegralIns, {
    //   foreignKey: 'integralId',
    //   sourceKey: 'id',
    // });
    // Integrals.hasMany(app.model.IntegralsOuts, {
    //   foreignKey: 'integralId',
    //   sourceKey: 'id',
    // });
  }

  return Integrals;

};